package com.bistu.xiong.common.app;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bistu.xiong.common.R;
import com.bistu.xiong.common.presenter.BaseContract;

public abstract class PresenterFragment<Presenter extends BaseContract.Presenter> extends Fragment implements BaseContract.View<Presenter>{
    protected Presenter mPresenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //在界面onAttach之后就触发初始化Presenter
        initPresenter();
    }

    /**
     * 初始化 Presenter
     * @return Presenter
     */
    protected abstract Presenter initPresenter();

    @Override
    public void showError(@StringRes int str) {
        //显示错误
        if(mPlaceHolderView!=null){
            mPlaceHolderView.triggerError(str);
        }else {
            Application.showToast(str);
        }
    }

    @Override
    public void showLoading() {
        if(mPlaceHolderView!=null){
            mPlaceHolderView.triggerLoading();
        }
    }

    @Override
    public void setPresenter(Presenter presenter) {
        // View中赋值Presenter
        mPresenter = presenter;
    }

    protected void toast(String str){
        Application.showToast(str);
    }
    protected void toast(@StringRes int msgId){
        toast(Application.getInstance().getString(msgId));
    }
}
